"""
实现的是：构建词典，实现方法把句子转化为数字序列和其翻转
"""


class Word2Sequence:
    UNK_TAG = "UNK"
    PAD_TAG = "PAD"
    UNK = 0
    PAD = 1
    def __init__(self):
        self.count = {} # 统计词频
        self.dict = {
            self.UNK_TAG:self.UNK,
            self.PAD_TAG:self.PAD

        }



    def fit(self,setence):
        """把单个句子保存在dict中

        :param setence:[word1,word2,word3...]
        :return:
        """
        for word in setence:
            self.count[word] = self.count.get(word,0) + 1 # get 获取key的值，不存在的话返回0

    def build_vocab(self,min=5,max=None,max_features=None):
        """
        生成词典
        :param min: 最小出现的次数
        :param max: 最大出现的次数
        :param max_features: 一共保留词语个数
        :return:
        """

        # 删除conut中词频小于 min的次
        if min is not None:
            self.count={word:value for word,value in self.count.items() if value>min}  #生成式筛选

        # 删除词频大于max的词
        if max is not None:
            self.count = {word:value for word,value, in self.count.items() if value<max}

        # 限制保留的词语数
        if max_features is not None:
            temp = sorted(self.count.items(),key = lambda x:x[-1],reverse=True)[:max_features] # key是接收一个函数
            self.count = dict(temp)


        for word in self.count:
            self.dict[word]=len(self.dict)  # 每个key对应的是字典长度，达到+1的效果


        #得到一个反转的字典
        # self.invers_dict={value:word for word,value in self.dict } # 速度慢了

        self.inverse_dict = dict(zip(self.dict.values(),self.dict.keys()))


    def transform(self,sentence,max_len = None):
        """
        把句子转化成序列
        :param setence:
        :return:
        """
        if max_len is not None:
            if max_len >len(sentence):
                sentence =sentence+[self.PAD_TAG]*(max_len-len(sentence))  # 填充
            if max_len < len(sentence):
                sentence = sentence[:max_len]  # 裁剪


        for word in sentence:
            self.dict.get(word,self.UNK)

        return [self.dict.get(word,self.UNK) for word in sentence]

    def inverse_transform(self,indices):
        """
        把序列转化为句子
        :param indices:
        :return:
        """

        return [self.inverse_dict.get(idx) for idx in indices]


    def __len__(self):
        return len(self.dict)



if __name__ == "__main__":
    # ws = Word2Sequence()
    # ws.fit(["我","是","谁"])
    # ws.fit(["我","是","我"])
    # ws.build_vocab(min=0)
    #
    # print(ws.dict)
    # ws.fit(["我","爱","北京"])
    # ret =ws.transform(["我","爱","北京"],max_len=10)
    # print(ret)
    # ret = ws.inverse_transform(ret)
    # print(ret)
    pass

